package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.MsgStatus;
import com.cloudfun.campusshare.common.constant.MsgType;
import com.cloudfun.campusshare.common.model.vo.MessageVo;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * Created by Huqin on 2019/12/28
 */
@RestController
@RequestMapping("/v1/message")
@Api(value = "消息模块", description = "消息模块", position = 1)
@AuthRequired
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/list")
    @ApiOperation("消息列表")
    @AuthRequired
    BaseResult<PageResult<MessageVo>> search(Pageable pageable,
                                             @ApiParam("消息状态") @RequestParam(value = "msgStatus", required = false) MsgStatus msgStatus,
                                             @ApiParam("消息类型") @RequestParam(value = "msgType", required = false) MsgType msgType,
                                             @ApiParam("搜索值") @RequestParam(value = "value", required = false) String value) {
        return BaseResultFactory.produceResult(Code.SUCCESS, messageService.search(value, msgStatus, msgType, pageable));
    }

    @GetMapping("/{id}")
    @ApiOperation("查看消息详情")
    @AuthRequired
    BaseResult<MessageVo> detail(@ApiParam(value = "id", required = true) @NotEmpty(message = "id不能为空") @PathVariable("id") String id) {
        return BaseResultFactory.produceResult(Code.SUCCESS, messageService.messageDetail(id));
    }

    @PostMapping("/markAsRead")
    @ApiOperation("消息已读")
    @AuthRequired
    BaseResult<?> markAsRead(@ApiParam(value = "ids", required = true) @NotEmpty(message = "消息id不能为空") @RequestBody List<String> ids) {
        messageService.markAsRead(ids);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @DeleteMapping
    @ApiOperation("删除消息")
    @AuthRequired
    BaseResult<?> delete(@ApiParam(value = "ids", required = true) @NotEmpty(message = "消息id不能为空") @RequestBody List<String> ids) {
        return messageService.updateEnabled(ids, false);
    }
}
